package com.happyclub.lbj.service.impl;

import com.happyclub.lbj.service.IOrderProduct;
import com.happyclub.lbjdao.*;
import com.happyclub.paul.service.IPhotoLibraryService;
import com.happyclub.pojo.*;
import com.happyclub.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;


/**
 * @author 高欢
 * @description TODO
 * @date 2019/4/12
 */
@Service
public class OrderProImpl implements IOrderProduct {
    @Autowired
    OrderProductDao iOrderProductDao;
    @Autowired
    ShopCarDao iShopCarDao;
    @Autowired
    OrderProductDetailDao iOrderProductDetailDao;
    @Autowired
    Product iProduct;
    @Autowired
    OrderDao orderDao;
    @Autowired
    IPhotoLibraryService iPhotoLibraryService;//paul

    @Transactional
    @Override
    public JsonResult saveOrderPro(String shopCarId) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        String userId = "";
        Double totalPrice = 0d;
        String[] split = shopCarId.split(",");
        if (split.length>0){
            for (String s:split){
                TbShopcar shopcar = iShopCarDao.findById(s).get();
                TbProduct byId = iProduct.findById(shopcar.getProductId()).get();
                userId = shopcar.getUserId();
                totalPrice = totalPrice + shopcar.getNum()*byId.getPrice();
            }
            TbRoderProduct tbRoderProduct = new TbRoderProduct();
            tbRoderProduct.setUserId(userId);
            tbRoderProduct.setTotalPrice(totalPrice);
            //插入orderProduct表
            TbRoderProduct save = iOrderProductDao.save(tbRoderProduct);
            for (String s:split){
                TbOrderProductDetail tbOrderProductDetail = new TbOrderProductDetail();
                tbOrderProductDetail.setOrderId(save.getOrderId());
                TbShopcar shopcar = iShopCarDao.findById(s).get();
                TbProduct byId = iProduct.findById(shopcar.getProductId()).get();
                tbOrderProductDetail.setProductId(shopcar.getProductId());
                tbOrderProductDetail.setPrice(byId.getPrice());
                tbOrderProductDetail.setNum(shopcar.getNum().intValue());
                tbOrderProductDetail.setColor(shopcar.getColor());
                tbOrderProductDetail.setSizee(shopcar.getSize());
                tbOrderProductDetail.setMoney(byId.getPrice()*shopcar.getNum());
                iOrderProductDetailDao.save(tbOrderProductDetail);
        }
            //成功之后删除已经下单的商品
            for (String s:split){
                iShopCarDao.deleteById(s);
            }
            jsonResult.setResult(save);
            return jsonResult;
        }
        jsonResult.setMsg("您还没有选择商品...");
        return jsonResult;
    }

    @Transactional
    @Override
    public JsonResult saveOrder(TbOrder order) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        TbOrder save = orderDao.save(order);
        jsonResult.setResult(save);
        return jsonResult;
    }

    @Transactional
    @Override
    public JsonResult statisticsProSalesVolume(String orderId) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        List<TbOrderProductDetail> byOrderId = iOrderProductDetailDao.findByOrderId(orderId);
        for (TbOrderProductDetail opd:byOrderId){
            iProduct.updateSellNum(opd.getNum(),opd.getProductId());
        }
        return jsonResult;
    }

    @Transactional
    @Override
    public JsonResult delAllByOrderId(String orderId) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        orderDao.deleteById(orderId);
        iOrderProductDetailDao.deleteTbOrderProductDetailByOrderId(orderId);
        iOrderProductDao.deleteById(orderId);
        return jsonResult;
    }

    @Override
    public JsonResult findOrderProByUserId(String userId) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        List<TbRoderProduct> tbRoderProductsByUserId = iOrderProductDao.findTbRoderProductsByUserId(userId);
        jsonResult.setResult(tbRoderProductsByUserId);
        return jsonResult;
    }

    @Override
    public JsonResult findOrderProDetailByOrderId(String orderId) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        List<TbOrderProductDetail> byOrderId = iOrderProductDetailDao.findByOrderId(orderId);
        for (TbOrderProductDetail opd:byOrderId){
            TbProduct tbProduct = iProduct.findById(opd.getProductId()).get();
            String[] split = tbProduct.getImgUrl().split(",");
            String imgUrl = "";
            for(String s:split){
               imgUrl = imgUrl + iPhotoLibraryService.queryPhotoLibraryByPhotoId(s).getPhotoUrl()+",";
            }
            tbProduct.setImgUrl(imgUrl);
            opd.setTbProduct(tbProduct);
        }
        jsonResult.setResult(byOrderId);
        return jsonResult;
    }

}
